package com.easyctrl.dialog;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;  
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.easyctrl.iface.OnDateListener;
import com.easyctrl.iface.OnTransmitText;
import com.easyctrl.ldy.activity.R;
import com.easyctrl.ldy.calendar.CalendarPickerView;
import com.easyctrl.ui.base.BaseDialog;

public class DateDialog extends BaseDialog implements OnDateListener,OnClickListener {

	private TextView textView; 
	private String dateStr;
	private OnTransmitText onTransmitText; 
	private Date date;
	public void setOnTransmitText(OnTransmitText onTransmitText) {
		this.onTransmitText = onTransmitText;
	}
	public DateDialog(Context context) {
		super(context);
		setContentView(R.layout.layout_date);
		textView = (TextView) findViewById(R.id.selectDate);
		findViewById(R.id.cancel).setOnClickListener(this);
		findViewById(R.id.confirm).setOnClickListener(this);
		Calendar nextYear = Calendar.getInstance();
		nextYear.add(Calendar.YEAR, 1);

		CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
		calendar.setOnDateListener(this);
		calendar.init(new Date(), new Date(), nextYear.getTime()); 
	} 

	@Override
	public void onSelectDate(Date time) {   
		this.date = time;
		dateStr = mContext.getResources().getString(R.string.dateSelect)+DateFormat.getDateInstance().format(this.date);
		textView.setText(dateStr);
	}
      
	@Override
	public void onClick(View v) { 
		if(v.getId() == R.id.confirm){
			Date d = new Date(System.currentTimeMillis());
			if(this.date == null){
				this.date = d;
				dateStr = mContext.getResources().getString(R.string.dateSelect)+DateFormat.getDateInstance().format(this.date);
			}
			onTransmitText.transmit(dateStr, OnTransmitText.TRANSMIT_R, OnTransmitText.TIME_TYPE_SPECIAL_TEXT,date);	
			dismiss();
		}else if(v.getId() == R.id.cancel){
			dismiss();
		}
	} 
}
